﻿using ApplicationModels.A04.Interactions;
using AutoMapper;
using Commons.Responses;
using EntityModels.E04.Interactions;
using IApplicationServices;
using IRepositories;

namespace ApplicationService
{
    public class CommentService : BaseApplicationService<int, Comment, CommentAM>, ICommentService
    {
        public readonly ICommentRepository _commentRepository;
        
        public CommentService(ICommentRepository repository, IMapper mapper) : base(repository, mapper)
        {
            _commentRepository = repository;
        }

        public Task<CommentAM> GetCommentByIdAsync(int commentId)
        {
            throw new NotImplementedException();
        }

        public async Task<DatasResponse<CommentAM>> GetCommentsByPhotographIdAsync(int pageIndex, int pageSize, string photographId)
        {
            var comments = await _commentRepository.GetCommentsByPhotographIdAsync(pageIndex,pageSize,photographId);
            var commentAMs = Mapper.Map<List<CommentAM>>(comments);

            foreach (var commentAM in commentAMs)
            {
                var replies = await _commentRepository.GetRepliesByCommentIdAsync(commentAM.Id);
                commentAM.Children = Mapper.Map<List<CommentAM>>(replies);
            }
            return new DatasResponse<CommentAM>(commentAMs);
        }

        public async Task<MessageResponse> IncreaseLikeAsync(int commentId)
        {
            return await _commentRepository.IncreaseLikeAsync(commentId);
        }

        public async Task<MessageResponse> ReduceLikeAsync(int commentId)
        {
            return await _commentRepository.ReduceLikeAsync(commentId);
        }
    }
}
